package com.google.code.jtasks.server.dao.mongo;

import com.google.code.jtasks.common.data.Task;
import com.google.code.jtasks.common.data.User;
import com.google.code.jtasks.server.dao.UserDAO;
import com.google.code.jtasks.server.modules.BindingDBUsers;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.inject.Inject;
import com.google.inject.Provides;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: a c e r
 * Date: 12.05.13
 * Time: 17:49
 * To change this template use File | Settings | File Templates.
 */
public class MongoUserDAO implements UserDAO {
    private static final Logger LOG = LoggerFactory.getLogger(MongoUserDAO.class);
    private final DB userDatabase;


     @Inject
    public MongoUserDAO(DB userDatabase) {
        this.userDatabase = userDatabase;
    }

    @Override
    public Optional<User> loadUser(String name) {
        Preconditions.checkNotNull(name, "User id is null");

        LOG.debug("Loading task from database: User name {}", name);

        DBCollection usersCollection = userDatabase.getCollection("users");
        DBObject objectLogin = usersCollection.findOne(new BasicDBObject("name", new ObjectId(name)));
         if (objectLogin == null) {
            return Optional.absent();
        } else {
            return Optional.of(fromDBObject(objectLogin));
        }
    }

    @Override
    public List<User> loadAllUsers() {
        DBCollection userDatabaseCollection = userDatabase.getCollection("users");

        LOG.debug("Loading all users from database");

        ImmutableList.Builder<User> users = ImmutableList.builder();
        for (DBObject object : userDatabaseCollection.find()) {
            users.add(fromDBObject(object));
        }

        return users.build();
    }

    public static User fromDBObject(DBObject dbObject) {
        Preconditions.checkNotNull(dbObject, "DB object is null");

        return new User(
                Optional.of(dbObject.get("id").toString()),
                (String) dbObject.get("name"),
                (String) (dbObject.get("password"))
        );
    }
    @Override
    public boolean authenticationUser(String userName,String password){

        DBCollection usersCollection = userDatabase.getCollection("users");
        DBObject objectLogin = usersCollection.findOne(new BasicDBObject("name", new ObjectId(userName)));
        DBObject objectPassword = usersCollection.findOne(new BasicDBObject("password", new ObjectId(password)));
        if (objectLogin!=null&&objectPassword!=null){
            return true;
        } else
       return false;
    }

}
